<template>
  <!-- 设备管理 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="设备IP地址" prop="equipIp">
        <el-input v-model="queryParams.equipIp"></el-input>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipname">
        <el-input v-model="queryParams.equipname"></el-input>
      </el-form-item>
      <el-form-item label="所属单位" prop="equipOrg">
        <el-input v-model="queryParams.equipOrg"></el-input>
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-select clearable v-model="queryParams.riskLevel" placeholder="全部" class="align mainInput">
          <el-option v-for="item in dict.type.equipment_risklevel" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-dropdown style="margin-left: 10px;" class="align align-right">
                  <el-button size="mini" type="primary" class="sysbtn">
                    下载<i class="el-icon-arrow-down el-icon--right"/>
                  </el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>csv</el-dropdown-item>
                    <el-dropdown-item>xlxs</el-dropdown-item>
                    <el-dropdown-item>json</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown> -->
      </el-form-item>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
            >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="!selectedRows.length"
          @click="handleBatchDelete"
          v-hasPermi="['system:notice:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['system:notice:export']"
        >导出</el-button
      >
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        label="IP地址"
        width="150"
        show-overflow-tooltip
        sortable
      >
        <template slot-scope="scope">
          {{ scope.row.equipIp }}
        </template>
      </el-table-column>
      <el-table-column
        prop="equipname"
        label="名称"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="equipOrg"
        label="所属单位"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="equiptype"
        label="类型"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="equipAddress"
        label="IP地址归属地"
        show-overflow-tooltip
      />

      <el-table-column
        label="真实坐标"
        width="150"
        show-overflow-tooltip
        sortable
      >
        <template slot-scope="scope">
          {{ scope.row.equipLon }},{{ scope.row.equipLat }}
        </template>
      </el-table-column>
      <el-table-column
        label="估计坐标"
        width="150"
        show-overflow-tooltip
        sortable
      >
        <template slot-scope="scope">
          {{ scope.row.beforeEquipRiskLon }},{{ scope.row.beforeEquipRiskLat }}
        </template>
      </el-table-column>
      <el-table-column prop="riskLevel" label="风险等级" show-overflow-tooltip  width="150">
        <template slot-scope="scope">

        <span  v-for="dict in dict.type.equipment_risklevel">
          <span v-if="scope.row.riskLevel==dict.value">
            {{dict.label}}
          </span>
        </span>

        <span  v-for="dict in dict.type.equipment_risklevel">
          <span v-if="scope.row.afterRiskLevel==dict.value">
            >>> {{dict.label}}
          </span>
        </span>
        </template>
      </el-table-column>
        <el-table-column prop="riskMark" label="标识" show-overflow-tooltip  width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.riskMark == '0'">未定位</span>
          <span v-if="scope.row.riskMark == '1'">定位已完成</span>
          <span v-if="scope.row.riskMark == '2'">抗定位已完成</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="beforeRiskDistance"
        label="抗定位前距离"
        show-overflow-tooltip
      >
        <template slot-scope="scope"> 
          <!-- 只保留两个小数点  sting数字-->
          {{ scope.row.beforeRiskDistance?Number(scope.row.beforeRiskDistance).toFixed(2) :'--' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="afterRiskDistance"
        label="抗定位后距离"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <!-- 只保留两个小数点  sting数字-->
          {{ scope.row.afterRiskDistance?Number(scope.row.afterRiskDistance).toFixed(2) :'--' }}
        </template>
      </el-table-column>

      <!-- <el-table-column prop="industry" label="所属行业" show-overflow-tooltip/> -->
      <el-table-column
        prop="operation"
        label="操作"
        width="250"
      >
        <template slot-scope="scope">
          <!-- <el-button type="text">
                详情
              </el-button> -->
<!--          v-if="scope.row.riskMark != '1'&&scope.row.afterRiskLevel!='1'"-->
          <el-button  type="text" @click="toantiLocalizationdetail(scope.row)" >
            抗定位
          </el-button>
          <el-button
            type="text"
            style="color: green"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            style="color: green"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-button>
          <!-- 中高风险显示抗定位按钮 -->
           <!-- <el-button v-if="scope.row.riskLevel == '3'" type="text" style="color: blue" @click="toantiLocalizationdetail(scope.row)">
            抗定位
          </el-button> -->
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPageChange"
      :current-page="queryParams.pageNum"
      :page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: center"
    >
    </el-pagination>
    <!-- 新增弹窗 -->
    <el-dialog title="新增设备信息" :visible.sync="dialogVisible1" width="30%">
      <div>
        <!-- 前两行 -->
        <el-row :gutter="20" class="form">
          <el-col :span="4">
            <div class="grid-content bg-purple">设备IP地址：</div>
          </el-col>
          <el-col :span="8">
            <el-input v-model="input1" placeholder="请输入" />
          </el-col>
          <el-col :span="4">
            <div class="grid-content bg-purple">设备归属组织：</div>
          </el-col>
          <el-col :span="8">
            <el-input v-model="input2" placeholder="请输入" />
          </el-col>
        </el-row>
        <el-row :gutter="20" class="form">
          <el-col :span="4">
            <div class="grid-content bg-purple">设备类型：</div>
          </el-col>
          <el-col :span="8">
            <el-input v-model="input3" placeholder="请输入" />
          </el-col>
          <el-col :span="4">
            <div class="grid-content bg-purple">状态：</div>
          </el-col>
          <el-col :span="8">
            <el-input v-model="input4" placeholder="请输入" />
          </el-col>
        </el-row>
        <!-- 后两行 -->
        <el-row :gutter="20" class="form">
          <el-col :span="4">
            <div class="grid-content bg-purple">设备信息描述(选填)：</div>
          </el-col>
          <el-col :span="20">
            <el-input v-model="input5" placeholder="请输入" />
          </el-col>
        </el-row>
        <el-row :gutter="20" class="form">
          <el-col :span="4">
            <div class="grid-content bg-purple">备注：</div>
          </el-col>
          <el-col :span="20">
            <el-input v-model="input6" placeholder="请输入" />
          </el-col>
        </el-row>
      </div>

      <!-- 两个按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible1 = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确认删除这些任务吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      :title="title + '设备'"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <div class="apk-detail-content">
        <el-card class="box-card">
          <el-form
            ref="formEquip"
            :model="formEquip"
            :rules="rules"
            label-width="110px"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item required label="设备名称" prop="equipname">
                  <el-input v-model="formEquip.equipname"></el-input>
                </el-form-item>
                <el-form-item required label="设备类型" prop="equiptype">
                  <el-input v-model="formEquip.equiptype"></el-input>
                </el-form-item>
                <el-form-item required label="所属单位" prop="equipOrg">
                  <el-input v-model="formEquip.equipOrg"></el-input>
                </el-form-item>
                <el-form-item required label="设备IP地址" prop="equipIp">
                  <el-input v-model="formEquip.equipIp"></el-input>
                </el-form-item>
                <el-form-item label="设备-国家" prop="equipCountry">
                  <el-input v-model="formEquip.equipCountry"></el-input>
                </el-form-item>
                <el-form-item label="设备-省" prop="equipState">
                  <el-input v-model="formEquip.equipState"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item required label="设备-城市" prop="equipCity">
                  <el-select  v-model="formEquip.equipCity" placeholder="设备-城市">
                    <el-option
                      v-for="dict in dict.type.ip_city"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item required label="设备-区" prop="equipArea">
                  <el-input v-model="formEquip.equipArea"></el-input>
                </el-form-item>
                <el-form-item required label="归属地" prop="equipAddress">
                  <el-input v-model="formEquip.equipAddress"></el-input>
                </el-form-item>
                <!-- <el-form-item label="估计坐标经度" prop="equipEstLon">
                  <el-input v-model="formEquip.equipEstLon"></el-input>
                </el-form-item>
                <el-form-item label="估计坐标纬度" prop="equipEstLat">
                  <el-input v-model="formEquip.equipEstLat"></el-input>
                </el-form-item> -->
                <el-form-item required label="真实坐标经度" prop="equipLon">
                  <el-input v-model="formEquip.equipLon"></el-input>
                </el-form-item>
                <el-form-item required label="真实坐标纬度" prop="equipLat">
                  <el-input v-model="formEquip.equipLat"></el-input>
                </el-form-item>
                <el-form-item required label="定位算法标识" v-if="title=='新增'">
                  <el-select  v-model="algorithmMark" placeholder="定位算法标识">
                    <el-option
                      v-for="dict in dict.type.equipment_algorithmmark"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="small"
                >提交</el-button
              >
              <el-button size="small" @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?algorithmMark=' + upload.algorithmMark"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
          <div>
            定位算法标识:
            <el-select  v-model="upload.algorithmMark" placeholder="定位算法标识" size="small">
                      <el-option
                        v-for="dict in dict.type.equipment_algorithmmark"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
          </div>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquip,
  addEquip,
  updateEquip,
  delEquip,
} from "@/api/networkEquipment/equipment";
import { getToken } from "@/utils/auth";
export default {
  dicts: ["ip_city", "sys_user_sex","equipment_risklevel","equipment_algorithmmark"],
  data() {
    return {
      selectedOption1: "",
      selectedOption2: "",
      selectedOption3: "",
      input1: "",
      input2: "",
      input3: "",
      input4: "",
      input5: "",
      input6: "",
      activeName: "first",
      dialogVisible: false,
      dialogVisible1: false,
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示的数据条数
      totalDataCount: 0, // 总数据量，初始为0，将在数据加载时设置
      selectedRows: [], // 存放选中的行
      tableData: [],
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
      },
      total: 0,
      addDialogVisible: false,
      formEquip: {},
      rules: {
        equipname: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        equiptype: [
          { required: true, message: "请输入设备类型", trigger: "blur" },
        ],
        equipOrg: [
          { required: true, message: "请输入所属单位", trigger: "blur" },
        ],
        equipIp: [
          { required: true, message: "请输入设备IP地址", trigger: "blur" },
        ],
        equipAddress: [
          { required: true, message: "请输入归属地", trigger: "blur" },
        ],
        equipLon: [
          { required: true, message: "请输入真实坐标经度", trigger: "blur" },
        ],
        equipLat: [
          { required: true, message: "请输入真实坐标纬度", trigger: "blur" },
        ],
        equipCity: [
          { required: true, message: "请输入设备城市", trigger: "blur" },
        ],
        equipArea: [
          { required: true, message: "请输入设备城区", trigger: "blur" },
        ],
      },
      title: "",
      algorithmMark:'1',
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 传参算法标识
        algorithmMark: '1',
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/equip/info/importEquipInfoData"
      },
    };
  },
  methods: {
    comP(data){
      //afterRiskLevel有值的时候先判断  没有值的时候判断riskLevel
      if(data.afterRiskLevel){
        if(data.afterRiskLevel=='3'||data.afterRiskLevel=='2'){
          return true;
        }else{
          return false;
        }
      }else{
        if(data.riskLevel=='3'||data.riskLevel=='2'){
          return true;
        }else{
          return false;
        }
      }

    },
    //抗定位
    toantiLocalizationdetail(row) {
      this.$router.push("/networkestimate?id=" + row.id);
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.title = "编辑";
      this.addDialogVisible = true;
      this.formEquip = JSON.parse(JSON.stringify(row));
    },
    // 详情
    handleDetail(row) {
      this.$router.push("/networkEquipment/equipment/detail?id=" + row.id);
    },
    // 取消
    cancel() {
      this.resetForm("formEquip");
      this.addDialogVisible = false;
    },
    // 提交
    onSubmit() {
      this.$refs["formEquip"].validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            this.formEquip.algorithmMark=this.algorithmMark
            addEquip(this.formEquip).then((res) => {
              if (res.code == 200) {
                this.getList();
                this.addDialogVisible = false;
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
              }
            });
          } else {
            updateEquip(this.formEquip).then((res) => {
              if (res.code == 200) {
                this.getList();
                this.addDialogVisible = false;
                this.$message({
                  type: "success",
                  message: "编辑成功!",
                });
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 新增
    handleAdd() {
      // this.resetForm("formEquip");
      this.formEquip = {};
      console.log(this.formEquip);
      this.addDialogVisible = true;
      this.title = "新增";
    },
    // 重置
    resetQuery() {
      this.resetForm("queryParams");
      this.getList();
    },
    // 检索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    // 获取列表
    getList() {
      listEquip(this.queryParams).then((res) => {
        this.tableData = res.rows || [];
        this.total = res.total;
      });
    },
    handleClick() {
      // 处理选项卡点击事件
    },

    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },

    handleDownload() {
      // 处理下载按钮点击事件
    },
    handleSizeChange(newSize) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = newSize;
      this.getList();
    },
    handleCurrentPageChange(newPage) {
      this.queryParams.pageNum = newPage;
      this.getList();
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          message: "请至少选择一个设备进行删除",
          type: "warning",
        });
        return;
      }

      this.$confirm("确认删除这些设备吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];
          this.selectedRows.map((ele) => {
            ids.push(ele.id);
          });
          delEquip(ids).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "批量删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            message: "已取消批量删除",
            type: "info",
          });
        });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delEquip(row.id).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equip/info/export",
        {
          ...this.queryParams,
        },
        `application_${new Date().getTime()}.xlsx`
      );
    },
     /** 导入按钮操作 */
     handleImport() {
      this.upload.title = "文件导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 下载导入模版
    importTemplate(){
      this.download('equip/info/download/template', {
      }, `equipment_template_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  },
  computed: {
    currentTableData() {
      let start = (this.currentPage - 1) * this.pageSize;
      let end = start + this.pageSize;
      return this.tableData.slice(start, end);
    },
  },

  mounted() {
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
.mainTable {
  padding: 15px;
  height: 85vh;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
}

.flex-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 垂直居中对齐 */
}

.btn {
  margin-left: 60px;
  background: linear-gradient(228deg, #ffc04b 0%, #ffa216 100%);
  color: #fff;
  border-radius: 4px 4px 4px 4px;
}

.flex-group-right {
  margin-left: 15px;
}

.rightBtn {
  margin-right: 55px;
  width: 106px;
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%);
  border-radius: 4px 4px 4px 4px;
}

.textStyle {
  margin-left: 5px;
  width: 96px;
  height: 22px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: #535a6c;
  line-height: 19px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

::v-deep .el-pager li.active {
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%) !important;
  color: white !important;
}

.search-icon {
  cursor: pointer;
  font-size: 18px;
  vertical-align: middle;
}
::v-deep .el-pagination {
  float: inline-end;
}
</style>
